package chatclient;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.jms.Destination;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class Chat extends JPanel {

    private static final String DATE_FORMAT_NOW = "HH:mm:ss";

    private String name = "";

    private JTextArea chat = new JTextArea();
    //JTextArea clients = new JTextArea("Clients\n");
    private JTextField chatInput = new JTextField();
    private JButton send = new JButton("Send!");
    private JButton close = new JButton("Lukk");
    private JScrollPane scroll = new JScrollPane(chat, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    
    private Destination destination;
    private MessageService mService;

    private GUI parent;

    public Chat(GUI parent, MessageService mService, Destination destination) {
        this.destination = destination;
        this.mService = mService;
        this.parent = parent;
        this.setLayout(new BorderLayout());


        chat.setEditable(false);

        /*clients.setEditable(false);
        clients.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));*/

        send.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                addLineToChat(chatInput.getText());
                sendMessage(chatInput.getText());
                chatInput.setText("");
            }
        });

        close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                close();
            }
        });

        chatInput.addKeyListener(new KeyListener() {

            public void keyPressed(KeyEvent e) {
                if (e.getKeyChar() == e.VK_ENTER) {
                    addLineToChat(chatInput.getText());
                    sendMessage(chatInput.getText());
                    chatInput.setText("");

                }
            }

            // Ubrukte abstrakte metoder
            public void keyTyped(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {
            }
        });

        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, chatInput, send);
        split.setDividerLocation(650);
        split.setOneTouchExpandable(false);

        this.add(scroll, BorderLayout.CENTER);
        this.add(close, BorderLayout.NORTH);
        //panel.add(clients, BorderLayout.EAST);
        this.add(split, BorderLayout.SOUTH);
        chatInput.requestFocusInWindow();
    }

    public void sendMessage(String text) {
        mService.sendMessage(destination, text);
    }

    public void addLineToChat(String msg) {
        if(!msg.trim().equals("")) {
            chat.append(currentTime() + "  You > " + msg +"\n");
        }
    }

    public void addStrangersLineToChat(String msg) {
        if(!msg.trim().equals("")) {
            chat.append(currentTime() + "  Stranger > " + msg +"\n");
        }
    }

    public void close() {
        parent.closeChat(this);
    }

    public boolean equals(Destination destination) {
        if(this.destination.toString().equalsIgnoreCase(destination.toString()) ) {
            return true;
        }
        else return false;
    }

    public static String currentTime() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        return sdf.format(cal.getTime());
    }
}
